//
//  LoginViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/21.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "LoginViewController.h"

#import "RegisterViewController.h"

@interface LoginViewController ()

@property (nonatomic,strong) UIView *topNavView;//顶部导航栏view

@property (nonatomic,strong) UIButton *backBtn;//返回btn

@property (nonatomic,strong) UIImageView *logoImgV;//logo

@property (nonatomic,strong) UITextField *phoneNumber_TF;//手机号tf

@property (nonatomic,strong) UITextField *verify_TF;//验证码tf

@property (nonatomic,strong) UIButton *getSmsBtn;//获取验证码btn

@property (nonatomic,strong) UIButton *loginBtn;//登录按钮

@property (nonatomic,strong) UIButton *registerBtn;//注册按钮

@property (nonatomic,strong) NSTimer *timer;//倒计时定时器

@property (nonatomic,assign) NSInteger count;//倒计时

@end

@implementation LoginViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self initUI];
}

-(void)initUI{
    [self.view addSubview:self.topNavView];
    [self.topNavView addSubview:self.backBtn];
    [self.view addSubview:self.logoImgV];
    
    float height = 10;
    
    NSArray *titles = @[@"手机号",@"验证码"];
    NSArray *placeHolders = @[@"请输入手机号",@"请输入验证码"];
    for (int i = 0; i<titles.count; i++) {
        UILabel *leftLB = [[UILabel alloc] initWithFrame:CGRectMake(50, self.logoImgV.bottom + 80 + i*60, 55, 20)];
        leftLB.font = FontSet(15);
        leftLB.textColor = MainBlackTextColor;
        leftLB.text = titles[i];
        [self.view addSubview:leftLB];
        //垂直线
        UIView *verticalLineV = [[UIView alloc] initWithFrame:CGRectMake(leftLB.right+10, leftLB.top+5, 1, 10)];
        verticalLineV.backgroundColor = MainPureColor(185);
        [self.view addSubview:verticalLineV];
        //水平线
        UIView *horizontalLineV = [[UIView alloc] initWithFrame:CGRectMake(50, leftLB.bottom+15, ScreenWidth-100, 1)];
        horizontalLineV.backgroundColor = MainPureColor(248);
        [self.view addSubview:horizontalLineV];
        
        UITextField *textF = [[UITextField alloc] initWithFrame:CGRectMake(verticalLineV.right+17, leftLB.top-5, (ScreenWidth-100)-83, 30)];
        textF.placeholder = placeHolders[i];
        textF.keyboardType = UIKeyboardTypeNumberPad;
        textF.font = FontSet(15);
        [self.view addSubview:textF];
        if (i == 0) {
            self.phoneNumber_TF = textF;
            self.phoneNumber_TF.rightView = [self rightViewWithWidth:textF.width];
            self.phoneNumber_TF.rightViewMode = UITextFieldViewModeAlways;
        }else if (i==1){
            self.verify_TF = textF;
        }
        
        if (i == titles.count - 1) {
            height = horizontalLineV.bottom;
        }
    }
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.registerBtn];
    self.loginBtn.top = height + 65;
    self.registerBtn.top = self.loginBtn.bottom+25;
}

-(UIView *)rightViewWithWidth:(float)width{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [view addSubview:self.getSmsBtn];
    return view;
}

#pragma mark - lazy
-(UIView *)topNavView{
    if (!_topNavView) {
        _topNavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, kHeight_NavBar)];
        _topNavView.backgroundColor = MainPureColor(246);
    }
    return _topNavView;
}
-(UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _backBtn.frame = CGRectMake(15, kHeight_StatusBar + 5, 35, 35);
        [_backBtn setImage:LoadImage(@"登录注册_back") forState:normal];
        [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

-(UIImageView *)logoImgV{
    if (!_logoImgV) {
        _logoImgV = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/2-32, self.topNavView.bottom+50, 64, 64)];
        _logoImgV.image = LoadImage(@"登录注册_logo");
    }
    return _logoImgV;
}
-(UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(51, kHeight_StatusBar + 5, ScreenWidth-100, 40);
        _loginBtn.clipsToBounds = YES;
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.backgroundColor = RGB(37, 130, 255);
        [_loginBtn setTitle:@"登录" forState:normal];
        [_loginBtn setTitleColor:MainPureColor(255) forState:normal];
        [_loginBtn.titleLabel setFont:FontSet(17)];
        [_loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}
- (UIButton *)registerBtn{
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _registerBtn.frame = CGRectMake(51, self.loginBtn.bottom + 25, ScreenWidth-100, 40);
        [_registerBtn setTitle:@"注册" forState:normal];
        [_registerBtn setTitleColor:RGB(37, 130, 255) forState:normal];
        [_registerBtn.titleLabel setFont:FontSet(17)];
        [_registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}
-(UIButton *)getSmsBtn{
    if (!_getSmsBtn) {
        _getSmsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _getSmsBtn.frame = CGRectMake(0, 0, 100, 30);
        [_getSmsBtn setTitle:@"获取验证码" forState:normal];
        [_getSmsBtn setTitleColor:RGB(37, 130, 255) forState:normal];
        [_getSmsBtn.titleLabel setFont:FontSet(12)];
        [_getSmsBtn.titleLabel setTextAlignment:NSTextAlignmentRight];
        [_getSmsBtn addTarget:self action:@selector(getVerifyCodeClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _getSmsBtn;
}
#pragma mark - SEL
-(void)backClick{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
//获取验证码
-(void)getVerifyCodeClick{
    [self.view endEditing:YES];
    if (self.phoneNumber_TF.text.length != 11) {
        [[XNProgressHUD shared] showWithTitle:@"请输入正确的手机号"];
        return;
    }
    //获取验证码
    [NetWorkLoginAndRegister getSmsCode:_phoneNumber_TF.text withSuccessBlock:^(id  _Nonnull responseObj) {
        [self startCountDownWithTime:60];//开启按钮倒计时
    }];
}
//开启定时器
-(void)startCountDownWithTime:(NSInteger)time{
    self.count = time;
    self.getSmsBtn.userInteractionEnabled = NO;
    [self.getSmsBtn setTitleColor:MainGrayTextColor forState:normal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo: nil repeats:YES];
}
- (void)timeDown{
    if (self.count != 1){
        self.count -= 1;
        self.getSmsBtn.userInteractionEnabled = NO;
        [self.getSmsBtn setTitle:[NSString stringWithFormat:@"剩余%ld秒", self.count] forState:UIControlStateNormal];
    } else {
        self.getSmsBtn.userInteractionEnabled = YES;
        [self.getSmsBtn setTitle:@"再次获取验证码" forState:UIControlStateNormal];
        [self.getSmsBtn setTitleColor:RGB(37, 130, 255) forState:normal];
        [self.timer invalidate];
        self.timer = nil;
    }
}
#pragma mark - 注册
-(void)loginClick{
    [self.view endEditing:YES];
    if (self.phoneNumber_TF.text.length != 11) {
        [[XNProgressHUD shared] showWithTitle:@"请输入正确的手机号"];
        return;
    }else if (self.verify_TF.text.length <= 0) {
        [[XNProgressHUD shared] showWithTitle:@"请输入验证码"];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.phoneNumber_TF.text forKey:@"telphone"];
    [params setValue:self.verify_TF.text forKey:@"code"];
    
    //登录方法
    [NetWorkLoginAndRegister loginWithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self backClick];
        });
    }];
}
//注册方法
-(void)registerClick{
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    vc.modalPresentationStyle = 0;
    [self presentViewController:vc animated:YES completion:nil];
    
    WEAKSELF(self);
    [vc setRegisterSuccessBlock:^{
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    
}

@end
